<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="../js/jquery-3.1.1.min.js"></script>
		<script type="text/javascript" src="../js/layui/layui.js">
		</script>
		<link rel="stylesheet" type="text/css" href="../js/layui/css/layui.css" />
	</head>
	<body>
<button type="button" class="layui-btn layui-btn-normal"  onclick="add()">添加</button>
	<table id="demo" lay-filter="test"></table>
 		<script type="text/html" id="barDemo">
 			<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="addmodules">权限设置</button>
			<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">修改角色信息</button>
			<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delet">删除</button>
 		</script>
		
	</body>
	<script>
	var token = window.localStorage.getItem("token");
	if(token==""||token==null){
		window.location="../login.html";
	}
$(function(){
	searchBtn();
})
var tableObj;
function searchBtn(){
layui.use(['table','jquery','form','tree'], function(){
  	  var table = layui.table;
	  var $ = layui.$;
	  var form = layui.form;
	  var tree = layui.tree;
  //第一个实例
  tableObj = table.render({
    elem: '#demo'
    ,method:'post'
    ,where:{
		token:token 
    }
    ,url: '../GetRolesAll' //数据接口
    ,page: true //开启分页
    ,cols: [[ //表头
      {field: 'id', title: '角色id',  sort: true, fixed: 'left'}
      ,{field: 'name', title: '角色名称', sort: true}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:240}
    ]]
  });
  	 //监听行工具事件
		   table.on('tool(test)', function(obj){
		     var data = obj.data;
		     document.getElementById("updates").reset();
		     //console.log(obj)
		     if(obj.event === 'edit'){
		    	 form.val("update",data)
		 	    	index = layer.open({
		 		    	 type:1
		 		    	 ,title:"角色名称修改"	
		 		    	 ,content:$("#update")
		 		    	 ,area:["",""]
		 		     })
		     } else if(obj.event === 'delet'){
		     	//alert(data.Id);
		     	 layer.confirm('确定要删除吗？', function(index){
		     	 	
		     	 	$.ajax({
						url:"../DeleteRole",
						data:{
							roleId:data.id,
							token:token
						},
						dataType:"json",
						type:"post",
						success:function(res){
							if(res.message==0){
								layer.msg(res.remark,function(){
									window.location="../login.html";
								});
							}else{
								layer.msg(res.remark,function(){
									window.location.reload();
								});
							}
						}
					})
		     	})
		     }else if(obj.event === 'addmodules'){
		     	//alert("添加权限模块");
		     	//var jsid = data.Id;
		     	$("#uuid").val(data.id);
		     	$.ajax({
						url:"../GetModulesByRoleIdLayUI",
						data:{
							roleId:data.id,
							token:token
						},
						dataType:"json",
						type:"post",
						success:function(res){
							if(res.message==0){
								layer.msg(res.remark,function(){
									window.location="../login.html";
								});
							}else{
								 //开启复选框
								  tree.render({
								    elem: '#test7'
								    ,id: 'demoIds'
								    ,data: res
								    ,showCheckbox: true
								  });
								
								index = layer.open({
				 		    	 type:1
				 		    	 ,title:"角色权限设置"	
				 		    	 ,content:$("#addmodwth")
				 		    	 ,area:["",""]
				 		     	})
							}
						}
					})
		     }
		   });
});
}
	//add
//点击添加按钮
// 角色添加
var index;
function add(){

	
	// alert("添加");
		layui.use("layer",function(){
			var layer = layui.layer;
			document.getElementById("adds").reset();
			index = layer.open({
	         
				type:1
				,title:"角色添加"
				,content:$("#add")
				,area:["",""]
			})
		}) 
}
//点击表单添加保存按钮
function addjs(){
	var jsname = $("#jsname").val();
  if(jsname==""){
		layer.msg("名称不能为空");
	}else{
		 $.post("../CreateRole?name="+jsname+"&token="+token,{},
	   	function(res){
	   		if(res.success){
	   			layer.msg("添加成功",function(){
	   				layer.close(index);
		   			tableObj.reload();
	   			});
	   		}else{
	   			if(res.message==0){
					layer.msg(res.remark,function(){
						window.location="../login.html";
					});
				}else{
					layer.msg("添加失败");
					layer.close(index);
					tableObj.reload();
				}
	   		}
	   	},"json"); 
	} 
}
//点击表单修改保存按钮
function updatejsbtn(){
	 //alert("修改保存");
	var updatejsid = $("#updatejsid").val();
	var updatejsname = $("#updatejsname").val();
	var updatejsnames = $("#updatejsnames").val();
	 if(updatejsname==""){
			layer.msg("名称不能为空");
		}else if(updatejsname==updatejsnames){
			layer.msg("名称还未修改哟");
		} else{
		 $.post("../UpdateRole?name="+updatejsname+"&roleId="+updatejsid+"&token="+token,{},
			   	function(res){
			   		if(res.success){
			   			layer.msg("修改成功");
			   			layer.close(index);
			   			tableObj.reload();
			   		}else{
			   			if(res.message==0){
							layer.msg(res.remark,function(){
								window.location="../login.html";
							});
						}else{
							layer.msg("修改失败");
							layer.close(index);
							tableObj.reload();
						}
			   		}
			   	},"json");  
		
		layer.close(index);
		tableObj.reload();
	}
}
//按钮事件			确定给予权限。
function addmodeqvanxan(){
	//alert("添加权限");
	var id = $("#uuid").val();
		layui.use(['tree', 'util'], function(){
		  var tree = layui.tree;
		  var layer = layui.layer;
		  var util = layui.util;
	      var checkedData = tree.getChecked('demoIds'); //获取选中节点的数据
	      var ids="";
	      
	      
	      //alert(cje);
	      //layer.alert(JSON.stringify(checkedData));
	      //alert(checkedData);
	      //console.log(checkedData);
	      //var jsons = $.parseJSON(checkedData);
	      //alert(checkedData。);
	      for(i in checkedData){
	      //for(i in checkedData){
	      ids += checkedData[i].id;
		  ids +=",";
		  var cje = checkedData[i].children;
	      	  for(o in cje){
	      	  	ids += cje[o].id;
	      	  	ids +=",";
	      	  }
	      }
	     var idss = ids.slice(0,-1);
	     //alert(ids);
			/*parentIds:功能模块id拼接的字符串(以逗号分隔)
			rId:要设置的角色的id
			token:数据库密钥*/
	
	$.ajax({
						url:"../SetSysRights",
						data:{
							parentIds:idss,
							rId:id,
							token:token
						},
						dataType:"json",
						type:"post",
						success:function(res){
							if(res.success){
								layer.msg("操作成功",function(){
									window.location.reload();
								});
							}else{
								if(res.message==0){
									layer.msg(res.remark,function(){
										window.location="../login.html";
									});
								}else{
									layer.msg("操作失败",function(){
										window.location.reload();
									});
								}
							}
						}
					})
	})
}
</script>
		<!--添加窗体  下 -->
		<div  id="add" style="display:none">
		
		   <form id="adds" class="layui-form">
		<div class="layui-inline">
			  <label class="layui-form-inline">角色名称：</label>
				<div class="layui-input-inline select">
				       <input type="text" id="jsname" lay-verify="required" placeholder="" autocomplete="off" class="layui-input"><br/>
				  </div>
		  </div>
		<div class="layui-inline count">
		    <button type="button" class="layui-btn layui-btn-warm"  onclick="addjs()">添加</button>
		    <button type="reset" class="layui-btn layui-btn-primary" >重置</button>
		</div>
		   </form>
		
		</div>         
<!--添加窗体 上 -->
<!--修改窗体  下 -->
		<div  id="update" style="display:none">
		
		   <form id="updates" class="layui-form"  lay-filter="update">
		    <input id="updatejsid" name="id" hidden="none" readonly="readonly">
		<div class="layui-inline">
			  <label class="layui-form-inline">角色名称：</label>
				<div class="layui-input-inline select">
				       <input name="name" id="updatejsname" type="text" id="peoplename" lay-verify="required" placeholder="人性姓名" autocomplete="off" class="layui-input"><br/>
				  		<input id="updatejsnames" name="name" hidden="none" readonly="readonly">
				  </div>
		  </div>
		<div class="layui-inline count">
		    <button type="button" class="layui-btn layui-btn-warm"  onclick="updatejsbtn()">修改</button>
		    <button type="reset" class="layui-btn layui-btn-primary" >重置</button>
		</div>
		   </form>
		
		</div>         
<!--修改窗体 上 --> 
<!--添加权限模块 下-->
		<div id="addmodwth" style="display: none;">
			<input type="text"  id="uuid"  style="display: none"/>
			 <button type="button" class="layui-btn layui-btn-warm"  onclick="addmodeqvanxan()">添加权限</button>
			 <div id="test7" class="demo-tree"></div>
		</div>
<!--添加权限模块 上-->
	</body>
</html>
